// pages/reportedit/reportedit.js
var App = getApp();
const chooseLocation = requirePlugin('chooseLocation');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		form:{
			pname:'',
			pphone:'',
			address:'',
			desc:'',
			stype:0
		},
		autosize:{
			minHeight:80
		},
		agree:1,
		InitCity:{
			popup:false,
			component:false,
			province:'',
			city:'',
			latitude:0,
			longitude:0,
			isprovince:0,
			iscity:0
		},
	},
	cityPopupOpen(){
		// this.setData({
		// 	"InitCity.popup":true,
		// 	"InitCity.component":true
		// });
		var latitude = wx.getStorageSync("latitude");
		var longitude = wx.getStorageSync("longitude");
		if(latitude&&longitude){
			this.OpentLocation(latitude,longitude)
		}else{
			wx.getLocation({
			    type: 'wgs84',
			    success :(res) => {
					this.OpentLocation(res.latitude,res.longitude)
			    },
				fail:(res) =>{
					wx.showModal({
						title: '提示',
						content: '位置请求频繁，请稍后重试！',
						showCancel:false,
						success (res) {
					    if (res.confirm) {
							// console.log('用户点击确定')
					    } else if (res.cancel) {
							// console.log('用户点击取消')
					    }
					  }
					})
				}
			})
		}
	},
	OpentLocation(latitude,longitude){
		const key = 'YBCBZ-WC4CU-P4EVZ-25TQK-UZAKE-XQBKR'; //使用在腾讯位置服务申请的key
		const referer = '75带班'; //调用插件的app的名称
		const location = JSON.stringify({
		  latitude: latitude,
		  longitude: longitude
		});
		
		wx.navigateTo({
		  url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&location=${location}`
		});
	},
	ChangeCityVal(val){
		this.setData({
			"InitCity.province":val.province,
			"InitCity.city":val.city,
			"InitCity.latitude":val.latitude,
			"InitCity.longitude":val.longitude,
			"form.address":val.address
		});
	},
	cityPopupClose(){
		this.setData({
			"InitCity.popup":false
		});
	},
	cityPopupAfterLeave(){
		this.setData({
			"InitCity.component":false
		});
	},
	onheaderClickLeft(){
		wx.navigateBack({})
	},

	handlepname(event){
		this.setData({
			'form.pname':event.detail
		});
	},
	handlepphone(event){
		this.setData({
			'form.pphone':event.detail
		});
	},
	handlepphone(event){
		this.setData({
			'form.pphone':event.detail
		});
	},
	handleaddress(event){
		this.setData({
			'form.address':event.detail
		});
	},
	handledesc(event){
		this.setData({
			'form.desc':event.detail
		});
	},
	handlestype(event){
		this.setData({
			'form.stype':event.detail
		});
	},

	handleSubmit(){
		var self = this;
		var data = self.data.form;
		if(!data.pname){
			wx.showToast({
				title: '名称不能为空',
				icon: 'error',
				duration: 2000
			})
			return false;
		}
		if(!data.pphone){
			wx.showToast({
				title: '手机号不能为空',
				icon: 'error',
				duration: 2000
			})
			return false;
		}
		var status = App.checkPhone(data.pphone);
		if(!status){
			wx.showToast({
				title: '手机号格式错误',
				icon: 'error',
				duration: 2000
			})
			return false;
		}
		if(!this.data.InitCity.province){
			wx.showToast({
				title: '地点不能为空',
				icon: 'error',
				duration: 2000
			})
			return false;
		}
		if(!data.address){
			wx.showToast({
				title: '地址不能为空',
				icon: 'error',
				duration: 2000
			})
			return false;
		}
		if(!data.desc){
			wx.showToast({
				title: '内容不能为空',
				icon: 'error',
				duration: 2000
			})
			return false;
		}
		const token = wx.getStorageSync('token');
		var param = {
			token:token,
			pname:data.pname,
			pphone:data.pphone,
			province:this.data.InitCity.province,
			city:this.data.InitCity.city,
			address:data.address,
			desc:data.desc,
			stype:data.stype,
		};
		
		wx.request({
			url: 'https://www.grabinter.com/Prosecute/addprosecute',
			method: 'POST',
			header: {
				"Content-Type": 'application/x-www-form-urlencoded'
			},
			data: param,
			success: json => {
				if(json.data.success){
					wx.showToast({
						title: '提交成功',
						icon: 'success',
						duration: 2000,
					});
					setTimeout(function(){
						wx.navigateBack({})
					},2000);
				}else{
					wx.showToast({
						title: '提交失败',
						icon: 'error',
						duration: 2000,
					});
					
				}
			}
		});
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		const location = chooseLocation.getLocation(); // 如果点击确认选点按钮，则返回选点结果对象，否则返回null
		if(location){
			console.log(location)
			this.ChangeCityVal({
				province:location.province,
				city:location.city,
				latitude:location.latitude,
				longitude:location.longitude,
				address:location.address
			});
		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
